package com.lhy.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;

import java.util.Arrays;

/**
 * @Author：罗昊宇
 * @Package：com.lhy.shiro
 * @Project：shiro-framwork
 * @name：Shiro01
 * @Date：2024/5/8 0008  15:28
 * @Filename：Shiro01
 */
public class Shiro01 {
    public static void main(String[] args) {
        DefaultSecurityManager securityManager=new DefaultSecurityManager();

        IniRealm realm =new IniRealm("classpath:shiro.ini");

        securityManager.setRealm(realm);

        SecurityUtils.setSecurityManager(securityManager);

        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token =new UsernamePasswordToken("lhy","123");

        try {
            subject.login(token);
            System.out.println("登录成功！");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("登录失败！");
        }
        //判断是否认证成功
        boolean authenticated = subject.isAuthenticated();
        if (authenticated){
            System.out.println("认证成功");
        }else {
            System.out.println("认证失败");
        }
        //判断是否具有user:query权限
        boolean permitted1 = subject.isPermitted("user:update");
        System.out.println("是否具有user:update权限:"+permitted1);
        boolean permitted2 = subject.isPermitted("user:query");
        System.out.println("是否具有user:query权限:"+permitted2);
        boolean[] permitted = subject.isPermitted("user:query", "user:aa");
        System.out.println("是否拥有:"+ Arrays.toString(permitted));
        boolean admin = subject.hasRole("admin");
    }
}
